package com.itheima.advice;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class RecordTimeAdvice {//Advice通知   增强
    @Pointcut("execution(* com.itheima.service.impl.*.*(..))")
    public void pt(){}

    @Around("pt()")
    public Object abc(ProceedingJoinPoint pjp) {
        long begin = System.currentTimeMillis();

        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Object object = null;

        try {
            object = pjp.proceed();//业务方法
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        long end = System.currentTimeMillis();
        log.info("执行了" + signature.getMethod().getName() + "方法==================耗时:" + (end - begin) + "毫秒");
        return object;
    }

    @Before("pt()")
    public void before() { //执行原方法之前
        log.info("执行了before方法........................");
    }

    @AfterReturning("pt()")
    public void afterReturning() {//正常执行原方法之后
        log.info("执行了afterReturning方法........................");
    }

    @AfterThrowing("pt()")
    public void afterThrowing() {//执行原方法出现了异常后
        log.info("执行了afterThrowing方法........................");
    }

    @After("pt()")
    public void after() {//最终要执行的方法 吗无论是否出现了异常
        log.info("执行了after方法........................");
    }

 }
